<?php

if ( ! function_exists('assets_url')) {
    function assets_url ($uri) {
        return base_url().ASSETSPATH.$uri;
    }
}

if ( ! function_exists('theme_url')) {
    function theme_url ($asset, $path) {
        $CI = &get_instance();
        if(isset($CI->layout))
            $layout = str_replace('.php', '', $CI->layout);
        else
            $layout = 'default';
        return base_url().ASSETSPATH.'themes/'.$layout.'/'.$asset.'/'.$path;
    }
}

if ( ! function_exists('img_theme')) {
    function img_theme ($path) {
        return theme_url('img', $path);
    }
}

if ( ! function_exists('css_theme')) {
    function css_theme ($path) {
        return theme_url('css', $path.'.css');
    }
}

if ( ! function_exists('jss_theme')) {
    function jss_theme ($path) {
        return theme_url('js', $path.'.js');
    }
}

if ( ! function_exists('module_url')) {
    function module_url ($asset, $path) {
        $CI = &get_instance();

        $segment_array = $CI->uri->segment_array();
        array_shift($segment_array);

        if($CI->uri->total_segments() == 0)
            $uri = '/home';
        else {
            if($asset != 'img')
                if ($CI->uri->segment(2) == 'manager')
                    $path = $CI->uri->segment(3).'.'.$asset;

            while(1) {

                $uri = NULL;
                foreach ($segment_array as $segment)
                    $uri = $uri.'/'.$segment;

                if ( ! is_dir(ASSETSPATH.'modules'.$uri))
                    array_pop($segment_array);
                else
                    break;
            }
        }
        return base_url().ASSETSPATH.'modules'.$uri.'/'.$asset.'/'.$path;
    }
}

if ( ! function_exists('img_module')) {
    function img_module ($path) {
        return module_url('img', $path);
    }
}

if ( ! function_exists('css_module')) {
    function css_module ($path = 'home') {
        return module_url('css', $path.'.css');
    }
}

if ( ! function_exists('js_module')) {
    function js_module ($path = 'home') {
        return module_url('js', $path.'.js');
    }
}

if ( ! function_exists('display_partial')) {
    function display_partial($theme, $partial) {
        return include('assets/themes/'.$theme.'/layout/'.$partial.'.php');
    }
}

if ( ! function_exists('display_menu')) {
    function display_menu() {
        $CI = &get_instance();

        if($CI->uri->segment(2) == 'manager')
            $CI->load->view($CI->uri->segment(3).'/'.$CI->uri->segment(3).'_menu');
        else
            $CI->load->view($CI->uri->segment(2).'_menu');

    }
}